package admin.sys;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.CommonUtil;
import com.common.MD5;
import com.common.ResultData;
import com.filter.BaseController;

import beans.sys.CityRegionBean;
import service.sys.CityRegionServiceImpl;

@Controller
@RequestMapping("/cityregion")
public class CityRegionController extends BaseController {
	@Autowired
	private CityRegionServiceImpl cityRegionServiceImpl;

	@RequestMapping("/list")
	public String Index(String type){
		return "admin/sys/cityregion/list";
	}

	@RequestMapping("/griddata")
	@ResponseBody
	public void DataGrid(CityRegionBean model,String condition,String keyword){
		ResultData<List<Map>> rd = new ResultData<>();
		try{
			if(!CommonUtil.isNullObject(condition)) {
				if(condition.equals("code"))model.setCode(keyword);
				else if(condition.equals("pcode"))model.setPcode(keyword);
			}
			rd.setPage(model.getPage());
			List<Map> m = cityRegionServiceImpl.queryByList(model);
			for(Map temM : m){
				CityRegionBean city = (CityRegionBean) cityRegionServiceImpl.queryById(temM.get("pcode"));
				if(city!=null)
					temM.put("parentName", city.getName());
			}
			rd.setRows(m);
			rd.setRecords(model.getPager().getRowCount());
			rd.setTotal(model.getPager().getPageCount());
		}
		catch(Exception e){
			e.printStackTrace();
		}
		responseJson(rd);
	}

	@RequestMapping("/del")
	@ResponseBody
	public void Del(String isuse){
		String[] ids = getReq().getParameterValues("ids");
		cityRegionServiceImpl.updateStatus(isuse,ids);
		ResultData<String> rd = new ResultData<>();
		rd.setSuccess(true);
		responseJson(rd);
	}

	@RequestMapping("/getSecondRegion")
	@ResponseBody
	public void getSecondRegion(String provinceid){
		List<CityRegionBean> areaList = cityRegionServiceImpl.findAll();
		List<Map<String,String>> cityArea = new ArrayList<>();
		for(CityRegionBean region : areaList){
			if(region.getPcode().equals(provinceid)){
				Map<String,String> map = new HashMap<>();
				map.put("value", region.getCode());
				map.put("text", region.getName());
				cityArea.add(map);
			}
		}
		responseJsonArray(cityArea);
	}
}
